package com.hymall.service.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hymall.bean.MarketLog;
import com.hymall.bean.MarketLogExample;
import com.hymall.bean.common.BasePageInfo;
import com.hymall.bean.common.CommonData;
import com.hymall.mapper.MarketLogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: 叶熠彬
 * @description: 操作日志信息
 * @date: 2022/7/17 13:22
 */

@Service
public class MarketLogServiceImpl implements MarketLogService {

    @Autowired
    MarketLogMapper logMapper;

    /**
     * 显示操作日志信息，用户名不为空时按用户名进行相似筛选
     *
     * @author: 叶熠彬
     * @date: 2022/7/17 13:34
     * @param: [name, info]
     * @return: com.hymall.bean.common.CommonData
     */
    @Override
    public CommonData queryLogList(String name, BasePageInfo info) {
        PageHelper.startPage(info.getPage(),info.getLimit(),info.getSort()+" "+info.getOrder());
        MarketLogExample example = new MarketLogExample();
        if (name != null && !"".equals(name)) {
            example.createCriteria().andAdminLike(name);
        }
        List<MarketLog> marketLogs = logMapper.selectByExample(example);
        PageInfo<MarketLog> pageInfo = new PageInfo<>(marketLogs);
        return CommonData.data(pageInfo);
    }

    /**
     * 增加日志信息
     *
     * @author: 叶熠彬
     * @date: 2022/7/17 17:08
     * @param: [log]
     * @return: int
     */
    @Override
    public int addLog(MarketLog log) {
        try {
            logMapper.insertSelective(log);
            return 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 404;
    }
}
